package middleware

import (
	"github.com/gin-gonic/gin"
	"github.com/satori/go.uuid"
)

const REQ_ID_KEY = "X-Request-Id"

func RequestId() gin.HandlerFunc {
	return func(c *gin.Context) {
		requestId := c.Request.Header.Get(REQ_ID_KEY)

		if requestId == "" {
			u4, _ := uuid.NewV4()
			requestId = u4.String()
		}

		c.Set(REQ_ID_KEY, requestId)

		c.Writer.Header().Set(REQ_ID_KEY, requestId)
		c.Next()

	}
}
